package com.si2.gui.model;

import java.awt.Color;

import jason.environment.grid.GridWorldModel;
import jason.environment.grid.Location;

public class AspiradorModel extends GridWorldModel {
	
	public static final String TITLE = "Aspirador";
	public static final int WINDOW_SIZE = 600;
	public static final int LIXOR = 16;
	public static final int LIXOG = 8;
	public static final int LIXOB = 32;
	public static final int uLIXO[] = {10,16,20};
	public static final int LIXO[] = {16,8,32};
	private boolean inicializacao = true;
	
	public static final int LARGURA = 8;
	public static final int ALTURA = 8;
	public static final int AGENTES = 6;
	public static final int T_SECONDS = 50;
	
	private int totalAspirado;
	public Location destino[] = new Location[AGENTES];
	public Color cor[] = new Color[AGENTES];
	
	public AspiradorModel(){
		super(LARGURA, ALTURA, AGENTES);
	}
	
    public void setDestinos() throws Exception
    {
    }
	
    public void moverDireita(int ag) throws Exception {
        Location aspLoc = getAgPos(ag);
		if (aspLoc.x < this.getWidth()-1) {
			aspLoc.x++;
        }
        setAgPos(ag, aspLoc);
	}
	
    public void moverEsquerda(int ag) throws Exception {
		
        Location aspLoc = getAgPos(ag);
		if (aspLoc.x > 0) {
			aspLoc.x--;
        }
        setAgPos(ag, aspLoc);
	} 
	
	public void moverCima(int ag) throws Exception {
        Location aspLoc = getAgPos(ag);
		if (aspLoc.y > 0) {
			aspLoc.y--;
        }
        setAgPos(ag, aspLoc);
	}    
	
    public void moverBaixo(int ag) throws Exception {
        Location aspLoc = getAgPos(ag);
		if (aspLoc.y < this.getHeight()-1) {
			aspLoc.y++;
        }
        setAgPos(ag, aspLoc);
	}      

    public void aspirar(int ag) {
        if (this.hasObject(LIXOR, getAgPos(ag))) {
        	totalAspirado++;
            remove(LIXOR, getAgPos(ag));
        }else if (this.hasObject(LIXOG, getAgPos(ag))) {
        	totalAspirado++;
            remove(LIXOG, getAgPos(ag));
        }else if (this.hasObject(LIXOB, getAgPos(ag))) {
        	totalAspirado++;
            remove(LIXOB, getAgPos(ag));
        }
    }
    
    public int getLixoEmVolta(int x, int y){
    	int retorno=0;
    	for(int i=-1; i<=1; i++)
    		for(int j=-1; j<=1; j++)
    			try{
    				if( !(i==0 && j==0) && (this.hasObject(LIXOR, x+i, y+j)
    						|| this.hasObject(LIXOG, x+i, y+j)
    						|| this.hasObject(LIXOB, x+i, y+j))
    						) retorno++;
    			}catch(Exception e){    				
    			}
    	return retorno;
    }

	public int getTotalAspirado() {
		return totalAspirado;
	}
	
	public boolean isInicializacao(){
		return inicializacao;
	}
	
	public void setInicializacao(boolean value){
		inicializacao = value;
	}
	
	public int getWidth(){
		return LARGURA;
	}
	
	public int getHeight(){
		return ALTURA;
	}
    
}
